package com.example.note;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.note.adapter.NotePadAdapter;
import com.example.note.bean.Note;
import com.example.note.db.MyHelper;

import java.util.List;

public class MainActivity extends AppCompatActivity {
    private ImageView add;
    private MyHelper myHelper;
    private List<Note> list;//存储查询出来的数据
    private ListView listView; //用于显示查询出来的数据
    private NotePadAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        add = findViewById(R.id.add);
        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this, RecordActivity.class);
                startActivityForResult(intent, 1);//数据回传
            }
        });
        listView = findViewById(R.id.listview);//用于显示查询出的记录的列表
        initData();
    }

    protected void initData() {
        myHelper = new MyHelper(this);//创建数据库
        showQueryData();//数据库查询
        //给item添加点击事件监听，完成修改交互
        listView.setOnItemClickListener((adapterView, view, i, l) -> {
            Intent intent = new Intent(MainActivity.this, RecordActivity.class);
            Note notepadBean = list.get(i);
            intent.putExtra("id", notepadBean.getId());
            intent.putExtra("content", notepadBean.getNotepadContent());
            intent.putExtra("notetime", notepadBean.getNotepadTime());
            //带值跳转到修改界面
            startActivityForResult(intent, 1);
        });
        listView.setOnItemLongClickListener((adapterView, view, i, l) -> {
            final AlertDialog dialog;
            AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this)
                    .setMessage("是否删除此记录？")
                    .setPositiveButton("确定", (dialogInterface, i1) -> {
                        Note notepadBean = list.get(i1);
                        if (myHelper.deleteData(notepadBean.getId())) {
                            list.remove(i1);
                            adapter.notifyDataSetChanged();
                            Toast.makeText(MainActivity.this, "删除成功", Toast.LENGTH_SHORT).show();
                        }
                    }).setNegativeButton("取消", (dialogInterface, i12) -> {
                        //dialog.dismiss();//关闭对话框
                    });
            dialog = builder.create();
            dialog.show();
            return true;
        });
    }

    private void showQueryData() {
        if (list != null) {
            list.clear();
        }
        //从数据库中查询数据
        list = myHelper.queryData();
        adapter = new NotePadAdapter(this, list);//定义适配器
        listView.setAdapter(adapter);//绑定适配器
    }

    //返回该界面时会自动调用onActivityResult方法
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        //更新界面
        if (requestCode == 1 && resultCode == 2) {
            showQueryData();
        }
    }
}
